﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using uPLibrary.Networking.M2Mqtt;
using uPLibrary.Networking.M2Mqtt.Messages;
using static uPLibrary.Networking.M2Mqtt.MqttClient;

namespace 完美外挂
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
            RegisterHotKey(this.Handle, 247696411, 0, (UInt32)Keys.F5); //注册热键
            RegisterHotKey(this.Handle, 247696410, 0, (UInt32)Keys.F6); //注册热键
        }

        [DllImport("user32.dll")]//取设备场景   
        private static extern IntPtr GetDC(IntPtr hwnd);//返回设备场景句柄   
        [DllImport("gdi32.dll")]//取指定点颜色   
        private static extern int GetPixel(IntPtr hdc, Point p);
        [DllImport("user32.dll")]
        public static extern UInt32 RegisterHotKey(IntPtr hWnd, UInt32 id, UInt32 fsModifiers, UInt32 vk); //API 

        int ButtonStatus;
        Thread t;

        private void Form1_Load(object sender, EventArgs e)
        {
            BloodXBox.Text = Properties.Settings.Default.BloodX;
            BloodYBox.Text = Properties.Settings.Default.BloodY;
            int r = int.Parse(Properties.Settings.Default.BloodR);
            int g = int.Parse(Properties.Settings.Default.BloodG);
            int b = int.Parse(Properties.Settings.Default.BloodB);
            pictureBlood.BackColor = Color.FromArgb(r, g, b);
            textBox1.Text = r.ToString() + "," + g.ToString() + "," + b.ToString();

            ShieldXBox.Text = Properties.Settings.Default.ShieldX;
            ShieldYBox.Text = Properties.Settings.Default.ShieldY;
            r = int.Parse(Properties.Settings.Default.ShieldR);
            g = int.Parse(Properties.Settings.Default.ShieldG);
            b = int.Parse(Properties.Settings.Default.ShieldB);
            pictureShield.BackColor = Color.FromArgb(r, g, b);

            Creat_Client("wanmeiR", "103.13.221.30", client_MqttMsgPublishReceived);

            t = new Thread(zidong);
            t.Start();
        }

        int huo;    //加火时间
        bool thread_flag = false;
        void zidong()
        {
            while (true)
            {
                if (thread_flag == false)
                {
                    goto end;
                }
                pictureBox1.BackColor = check_Mouse(int.Parse(BloodXBox.Text), int.Parse(BloodYBox.Text));

                this.Text = pictureBox1.BackColor.ToString();
                if ((pictureBox1.BackColor.R < pictureBlood.BackColor.R + 20) && (pictureBox1.BackColor.R > pictureBlood.BackColor.R - 20)&&
                    (pictureBox1.BackColor.G < pictureBlood.BackColor.G + 20) && (pictureBox1.BackColor.G > pictureBlood.BackColor.G - 20)&&
                    (pictureBox1.BackColor.B < pictureBlood.BackColor.B + 20) && (pictureBox1.BackColor.B > pictureBlood.BackColor.B - 20)
                    )
                {
                    SendKeys.SendWait("{1}");
                    // 发布消息到主题 "/home/temperature" 消息质量为 2,不保留   
                    client.Publish("wanmei", Encoding.UTF8.GetBytes("1"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
                }
                else
                {
                    SendKeys.SendWait("{Tab}");
                    client.Publish("wanmei", Encoding.UTF8.GetBytes("tab"), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
                }

                //开盾
                //pictureBox1.BackColor = check_Mouse(int.Parse(ShieldXBox.Text), int.Parse(ShieldYBox.Text));

                //if ((pictureBox1.BackColor.R < pictureShield.BackColor.R + 10) && (pictureBox1.BackColor.R > pictureShield.BackColor.R - 10))
                //{
                //    SendKeys.SendWait("{3}");
                //}
                Thread.Sleep(1000);

                //huo++;
                //if (huo % 10 == 0)
                //{
                //    SendKeys.SendWait("{3}");
                //}
                end:
                int i = 0;
            }
        }

        //重写消息循环 
        protected override void WndProc(ref Message m)
        {
            const int WM_HOTKEY = 0x0312;
            // m.WParam.ToInt32() 要和 注册热键时的第2个参数一样 
            if (m.Msg == WM_HOTKEY && m.WParam.ToInt32() == 247696411) //判断热键 
            {
                //写上你的代码 
                if (ButtonStatus == 1)
                {
                    pictureBlood.BackColor = pictureBox1.BackColor;
                    BloodXBox.Text = MousePoint.X.ToString();
                    BloodYBox.Text = MousePoint.Y.ToString();

                    Properties.Settings.Default.BloodX = BloodXBox.Text;
                    Properties.Settings.Default.BloodY = BloodYBox.Text;
                    Properties.Settings.Default.BloodR = MouseR.ToString();
                    Properties.Settings.Default.BloodG = MouseG.ToString();
                    Properties.Settings.Default.BloodB = MouseB.ToString();
                    Properties.Settings.Default.Save();
                }
                else if (ButtonStatus == 2)
                {
                    pictureShield.BackColor = pictureBox1.BackColor;
                    ShieldXBox.Text = MousePoint.X.ToString();
                    ShieldYBox.Text = MousePoint.Y.ToString();

                    Properties.Settings.Default.ShieldX = ShieldXBox.Text;
                    Properties.Settings.Default.ShieldY = ShieldYBox.Text;
                    Properties.Settings.Default.ShieldR = MouseR.ToString();
                    Properties.Settings.Default.ShieldG = MouseG.ToString();
                    Properties.Settings.Default.ShieldB = MouseB.ToString();
                    Properties.Settings.Default.Save();
                }

            }
            else if(m.Msg == WM_HOTKEY && m.WParam.ToInt32() == 247696410)
            {
                start();
            }
            base.WndProc(ref m);
        }

        Point MousePoint;
        int MouseR;
        int MouseG;
        int MouseB;

        private void timer1_Tick(object sender, EventArgs e)
        {
            MousePoint = new Point(MousePosition.X, MousePosition.Y);//取置顶点坐标       
            IntPtr hdc = GetDC(new IntPtr(0));//取到设备场景(0就是全屏的设备场景)   
            int c = GetPixel(hdc, MousePoint);//取指定点颜色   
            MouseR = (c & 0xFF);//转换R   
            MouseG = (c & 0xFF00) / 256;//转换G   
            MouseB = (c & 0xFF0000) / 65536;//转换B   
            pictureBox1.BackColor = Color.FromArgb(MouseR, MouseG, MouseB);

            this.Text = "(" + MousePoint.X + "," + MousePoint.Y + Color.FromArgb(MouseR, MouseG, MouseB) + ")";
        }

        //开始按钮
        private void start()
        {
            if (button1.Text == "开始")
            {
                button1.Text = "停止";
                thread_flag = true;
                timer2.Enabled = true;
            }
            else if (button1.Text == "停止")
            {
                button1.Text = "开始";
                thread_flag = false;
                timer2.Enabled = false;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            start();
        }

        private Color check_Mouse(int x,int y)
        {
            Point p = new Point(x, y);//取置顶点坐标       
            IntPtr hdc = GetDC(new IntPtr(0));//取到设备场景(0就是全屏的设备场景)   
            int c = GetPixel(hdc, p);//取指定点颜色   
            int r = (c & 0xFF);//转换R   
            int g = (c & 0xFF00) / 256;//转换G   
            int b = (c & 0xFF0000) / 65536;//转换B   
            return Color.FromArgb(r, g, b);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (button2.Text == "设置")
            {
                button2.Text = "停止";
                timer1.Enabled = true;
            }
            else if (button2.Text == "停止")
            {
                button2.Text = "设置";
                timer1.Enabled = false;
            }
        }

        private void timer4_Tick(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            button3.Enabled = false;
            button4.Enabled = true;
            button5.Enabled = true;
            ButtonStatus = 1;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            button3.Enabled = true;
            button4.Enabled = false;
            button5.Enabled = true;
            ButtonStatus = 2;
        }

        private void button5_Click(object sender, EventArgs e)
        {
            button3.Enabled = true;
            button4.Enabled = true;
            button5.Enabled = false;
            ButtonStatus = 3;
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            t.Abort();
            client.Disconnect();
        }

        bool PickFlag = true;
        private void button6_Click(object sender, EventArgs e)
        {
            if (PickFlag == false)
            {
                PickFlag = true;
                textBox2.Enabled = true;
            }
            else
            {
                PickFlag = false;
                textBox2.Enabled = false;
            }
        }

        bool 武器flag = false;
        private void timer2_Tick(object sender, EventArgs e)
        {
            if (武器flag == true)
            {
                武器flag = false;
                SendKeys.SendWait("{F1}");
                SendKeys.SendWait("{F2}");
            }
            else
            {
                武器flag = true;
                SendKeys.SendWait("{F3}");
                SendKeys.SendWait("{F4}");
            }
            SendKeys.SendWait("{3}");
        }

        MqttClient client;      //接收判断信息


        //创建客户端，此客户端是确定需要传输文件后才创建
        private void Creat_Client(string sub, string ipString, MqttMsgPublishEventHandler MqttEvent)
        {
            //创建客户端实例  
            client = new MqttClient(IPAddress.Parse(ipString)); //主机为IP时              
            //MqttClient client = new MqttClient(MQTT_BROKER_ADDRESS); //当主机地址为域名时  

            // 注册消息接收处理事件，还可以注册消息订阅成功、取消订阅成功、与服务器断开等事件处理函数  
            client.MqttMsgPublishReceived += MqttEvent;
            //生成客户端ID并连接服务器  
            string clientId = Guid.NewGuid().ToString();
            client.Connect(clientId);
            // 发布消息到主题 "Bookmarks" 消息质量为 2,不保留 
            client.Subscribe(new string[] { sub }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });
        }

        void client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e)
        {
            //处理接收到的消息  
            string msg = System.Text.Encoding.UTF8.GetString(e.Message);
            //textBox3.AppendText(msg + "\r\n");        
        }
    }
}
